package com.neusoft.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neusoft.crm.mapper.ClientInfoMapper;
import com.neusoft.crm.mapper.OrdersMapper;
import com.neusoft.crm.mapper.SysUserMapper;
import com.neusoft.crm.po.ClientInfo;
import com.neusoft.crm.po.Orders;
import com.neusoft.crm.po.SysUser;
import com.neusoft.crm.service.ClientInfoService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Log4j2
public class ClientInfoController {

    @Resource
    private ClientInfoMapper clientInfoMapper;
    @Resource
    private ClientInfoService clientInfoService;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private OrdersMapper ordersMapper;



    @RequestMapping("/selectAllClientInfo")
    public List<ClientInfo> selectAllClientInfo () {

          clientInfoMapper.updataselectAllClientInfo();
        return clientInfoMapper.selectAllClientInfo();
    }

    @RequestMapping("/selectAllchurnClientInfo")
    public List<ClientInfo> selectAllchurnClientInfo(){

        return clientInfoMapper.selectAllClientInfo();
    }

    @RequestMapping("/ClientChurnReasonSave")
    public int ClientChurnReasonSave(@RequestBody ClientInfo clientInfo){
        UpdateWrapper<ClientInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("client_Code",clientInfo.getClientCode());
        updateWrapper.set("client_State",3);
        clientInfoMapper.updateById(clientInfo);
        return clientInfoMapper.update(null, updateWrapper);
    }

    @RequestMapping("/churnPlanSuccess")
    public int clientState(@RequestBody ClientInfo clientInfo){
        UpdateWrapper<ClientInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("client_Code",clientInfo.getClientCode());
        updateWrapper.set("client_State",1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date currentDate = new Date();
        String formattedDate = sdf.format(currentDate);
        UpdateWrapper<Orders> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.eq("client_Code", clientInfo.getClientCode())
                .set("orders_Date",formattedDate);
        ordersMapper.update(null,updateWrapper1);
        return clientInfoMapper.update(null, updateWrapper);

    }

    @RequestMapping("/selectAllClientInfoBykejlUserId")
    public List<ClientInfo> selectAllClientInfoBykejlUserId (@RequestBody SysUser sysUser){
        QueryWrapper<ClientInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("client_Cust_Id",sysUser.getUserId());
        return clientInfoMapper.selectList(queryWrapper);
    }

}
